package cn.webrx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Ex6 {
    public static void main(String[] args) {
        game03(new File("c:/t1"), new File("c:/ttt2"));
    }

    //2、编写程序实现文件复制
    public static void game02(File sc, File sa) {
        try (
                var fis = new FileInputStream(sc);
                var fos = new FileOutputStream(sa)
        ) {
            byte[] buf = new byte[1024];
            int s = 0;
            while ((s = fis.read(buf)) != -1) {
                fos.write(buf, 0, s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //2、编写程序实现文件复制
    public static void game02to(File sc, File sa) {
        if (!sa.exists()) {
            sa.mkdirs();
        }
        //game02(sa, new File(sc.getName()));
        game02(sc, new File(sa.getAbsoluteFile() + "/" + sc.getName()));
    }

    //3、编写程序实现目录复制功能
    public static void game03(File sc, File dst) {
        if (!dst.exists()) {
            dst.mkdirs();
        }
        File[] fs = sc.listFiles();
        for (File f : fs) {
   if (f.isDirectory()) {
             game03(f, new File(dst.getAbsolutePath() + "/" + f.getName()));
            } else if (f.isFile()) {
                game02to(f, dst);
            }
        }
    }

}

